import {
	httpGet,
	httpPost,
	httpGetArraybuffer,
	httpRequest
} from "../../http/http.js"


// 测试服务器链接
export const testLink = () => httpGet("/test/test")

// 测试后台连接,get/post都可以,根据header中的 token 判断能否登录后台 
export const testRightPost = () => httpPost("/admin/testRight")
export const testRight = () => httpGet("/admin/testRight")


// 获取验证码
export const getKaptchaCode = () => httpGet("/test/getKaptchaCode")

// 获取验证码图片
export const getKaptcha = () => {
	return httpGetArraybuffer("/kaptcha?t=" + new Date()).then(res => {
		// 保存cookie
		if (res.header["Set-Cookie"] != undefined && res.header["Set-Cookie"] !== "") {
			// 截取第一个;前面的字符串就是jsessionid
			uni.setStorageSync('cookies', res.header["Set-Cookie"].split(';')[0]);
		}
		// 将二进制转成arrayBuffer类型
		const arrayBuffer = new Uint8Array(res.data)
		//  buffer转成base64 
		const imgBase64 = uni.arrayBufferToBase64(arrayBuffer)
		// bs464 设置值,再用计算属性设置到src上
		return Promise.resolve(imgBase64);
	}).catch(error => {
		return Promise.reject(error);
	})
}

// 获取session
export const getSession = () => httpGet("/test/getSession")

export default{
	testLink,
	testRightPost,
	testRight,
	getKaptchaCode,
	getKaptcha,
	getSession,
}